package com.example.administrator.foodrowfresh.jiao;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.example.administrator.foodrowfresh.R;
import com.example.administrator.foodrowfresh.jiao.adapter.PromotionItemAdapter;
import com.example.administrator.foodrowfresh.jiao.modle.PromotionItemGVModel;
import com.example.administrator.foodrowfresh.jiao.modle.PromotionItemModel;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PromotionItemActivity extends AppCompatActivity {
    private PullToRefreshListView mListView = null;
    private List<PromotionItemModel.DataBean> mDatas;
    private BaseAdapter mAdapter;
    //PullToRefreshListView
    private ILoadingLayout loadingLayoutProxy;
    private List<Map<String, String>> list1;
    private View mView;
    Map<String, String> map;
    //监听点击id
    private String idStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_promotion_item);
        initView();
    }

    private void initView() {

        mListView = (PullToRefreshListView) findViewById(R.id.promotion_activity_pulltorefreshlistview);
        mDatas = new ArrayList<>();
        list1 = new ArrayList<>();
        //绑定监听
        mAdapter = new PromotionItemAdapter(mDatas, list1, this);

        mListView.setAdapter(mAdapter);
        getDatas();
        getGridData();

    mListView.setEnabled(false);
    }
    public void getDatas() {
        RequestParams params = new RequestParams(NetConfigPromotion.PROMOTION_ITEM_TITLE_PATH);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();

                PromotionItemModel test = gson.fromJson(result, PromotionItemModel.class);
                PromotionItemModel.DataBean data = test.getData();
                mDatas.add(data);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void getGridData() {

        RequestParams params = new RequestParams(NetConfigPromotion.PROMOTION_ITEM_PATH);
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Toast.makeText(PromotionItemActivity.this, "网络访问成功", Toast.LENGTH_SHORT).show();
                Gson gson = new Gson();
                PromotionItemGVModel promotionItemGVModel = gson.fromJson(result, PromotionItemGVModel.class);
                List<PromotionItemGVModel.DataBean.ListBean> list = promotionItemGVModel.getData().getList();

                for (int i = 0; i < list.size(); i++) {
                    List<PromotionItemGVModel.DataBean.ListBean.ProductBean> product = list.get(i).getProduct();
                    for (int j = 0; j < product.size(); ++j) {
                        map = new HashMap<String, String>();
                        String name = product.get(j).getName();
                        String now_price = product.get(j).getNow_price() + "";
                        String image = product.get(j).getImage();
                        String weight = product.get(j).getWeight();
                        idStr = product.get(j).getId() + "";
                        map.put("id", idStr);
                        map.put("name", name);
                        map.put("now_price", now_price);
                        map.put("image", image);
                        map.put("weight", weight);
                        list1.add(map);
                    }
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(PromotionItemActivity.this, "网络访问不成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
